
import { createRouter, createWebHistory } from 'vue-router'

const routes = [
    {
    path: '/login',
    name: 'login',
    component: () => import('@/views/login/login.vue'),
    meta: {
      title: '登录',
    }
  },
  {
    path: '/',
    name: '首页',
    component: () => import('@/views/index/index.vue'),
    meta: {
      title: '首页',
      requiresAuth: false, // bu需要登录
    //   keepAlive: true
    }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 全局前置守卫
router.beforeEach((to, from, next) => {
    const isAuthenticated = localStorage.getItem('token') !== null;; // 检查用户是否登录
    if (to.meta.requiresAuth && !isAuthenticated) {
      // 如果需要登录且用户未登录，重定向到登录页面
      next('/login');
    } else {
      // 否则继续导航
      next();
    }
  });
  

export default router